<template>
    <div>
        <!--这里的ref可以在登录方法中获取所有的表单信息，类似于document....getbyid-->
        <el-form
                :rules="rules"
                :model="loginForm"
                class="loginContainer"
                v-loading="loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                ref="loginForm">
            <h3 class="loginTitle">系统登录</h3>
            <!--这里的prop用于校验-->
            <el-form-item size="normal" prop="username">
                <!--auto-complete:是否自动补全-->
                <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item size="normal" prop="password">
                <!--auto-complete:是否自动补全-->
                <el-input type="text" v-model="loginForm.password" auto-complete="off" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-checkbox v-model="checked" class="loginRember">记住我</el-checkbox>
            <el-button size="normal" type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>

    export default {
        name: "Login",
        //定义数据
        data() {
            return {
                loading: false,
                loginForm: {
                    username: "admin",
                    password: "123"
                },
                //是否记住
                checked: true,

                //校验规则
                rules: {
                    //required:true:用户名必填  如果没填就弹出““””“"请输入用户名",trigger:blur 触发的方式是blur
                    username: [{required: true, message: "请输入用户名", trigger: "blur"}],
                    password: [{required: true, message: "请输入密码", trigger: "blur"}]
                }
            }
        },
        methods: {
            submitLogin() {
                this.$refs.loginForm.validate((validate) => {
                    if (validate) {
                        this.loading=true;
                        /*参数是请求路径和请求参数，请求路径是服务器的路径，参数也是*/
                        this.postKeyValueRequest('/doLogin', this.loginForm).then(resp => {
                            this.loading=false;
                            //resp存在数据会进入这个判断
                            if (resp) {
                                this.$store.commit("INIT_CURRENTHR",resp.obj);
                                //将js的对象转成json对象(接收过程数据格式转换 json->key:val->json)
                                //alert(JSON.stringify(resp.data));
                                //利用sessionStorage获取服务端传递的信息
                                window.sessionStorage.setItem("user",JSON.stringify(resp.data));
                                //登录成功后进入要访问的页面
                                let path = this.$route.query.redirect;
                                this.$router.push((path==='/'||path===undefined)?"/home":path);
                                //跳转到home页。利用replace可以直接替换掉登录页的url,不能回退
                                //push可以回退，浏览器有回退按钮
                                //this.$router.replace("/home")
                            }
                        })
                    } else {
                        this.$message.error("请输入所有字段");
                        return false;
                    }
                })
            }
        }
    }
</script>

<style>
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .loginTitle {
        margin: 15px auto 20px auto;
        text-align: center;
        color: #505458;
    }

    .loginRember {
        text-align: left;
        margin: 0 0 25px 0;
    }

</style>